package common;

import database.Database;

public class CommonMethods {
    public static java.util.Date getMondayDate() {
        java.util.Date currentDate = new java.util.Date();
        int currentDayOfWeek = currentDate.getDay();    // 0-6, 0..Sunday
        if (currentDayOfWeek == 0) {
            currentDayOfWeek = 7;
        }
        int currentDayOfMonth = currentDate.getDate();  // 1-31
        
        java.util.Date mondayDate = currentDate;
        
        if (currentDayOfWeek >= 1 && currentDayOfWeek <= 5) {
            // this week's monday
            mondayDate.setDate(currentDayOfMonth - currentDayOfWeek + 1);
        } else {
            // next week's monday (if today is saturday or sunday)
            mondayDate.setDate(currentDayOfMonth - currentDayOfWeek + 1 + 7);
        }
        
        return mondayDate;
    }
    
    public static String generateNewUsername(String firstName, String lastName) throws java.sql.SQLException {
        String normalizedFirstName = java.text.Normalizer.normalize(firstName, java.text.Normalizer.Form.NFD);
        String normalizedLastName = java.text.Normalizer.normalize(lastName, java.text.Normalizer.Form.NFD);
        String username = normalizedFirstName.substring(0, 1).toLowerCase() + normalizedLastName.substring(0, 1).toLowerCase();
        
        char[] characters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
                             'o', 'p', 'q', 'r', 's' ,'t', 'u', 'v', 'w', 'x', 'y', 'z'};
        
        String suffix = "";
        java.util.Random indexGenerator = new java.util.Random();
        suffix = suffix + characters[indexGenerator.nextInt(characters.length)] + characters[indexGenerator.nextInt(characters.length)];
        
        java.util.Random numberGenerator = new java.util.Random();
        username = username + numberGenerator.nextInt(10) + numberGenerator.nextInt(10);
        
        
        for (int i = 0; i < 14; i++) {
            if (Database.usernameExists(username + suffix)) {
                username = username + numberGenerator.nextInt(10);
            } else {
                break;
            }
        }
        
        return username + suffix;
    }
    
    public static String generatePassword() {
        char[] characters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
                             'o', 'p', 'q', 'r', 's' ,'t', 'u', 'v', 'w', 'x', 'y', 'z',
                             'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 
                             'O', 'P', 'Q', 'R', 'S' ,'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                             '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        
        String password = "";
        java.util.Random indexGenerator = new java.util.Random();
        for (int i = 0; i < 4; i++) {
            password = password + characters[indexGenerator.nextInt(characters.length)];
        }
        
        return password;
    }
    
    public static String encrypt(String password) {
        java.security.MessageDigest encryptor;
        
        try {
            encryptor = java.security.MessageDigest.getInstance("MD5");
        } catch (java.security.NoSuchAlgorithmException exception) {
            return null;
        }
        
        encryptor.reset();
        encryptor.update(password.getBytes());
        byte[] digest = encryptor.digest();
        java.math.BigInteger bigInt = new java.math.BigInteger(1, digest);
        String passwordHash = bigInt.toString(16);
        // doplnia sa nuly na zaciatok, lebo bigInt ich na zaciatku neobsahuje
        while (passwordHash.length() < 32 ) {
            passwordHash = "0" + passwordHash;
        }
        
        return passwordHash;
        
    }

    private CommonMethods() {
    }
}
